<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>07-DOM操作练习：innerHTML的方式创建元素</title>
  
    
      <meta 
        property="og:title" 
        content="07-DOM操作练习：innerHTML的方式创建元素">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2008/08/01/08-%E5%89%8D%E7%AB%AF%E5%9F%BA%E6%9C%AC%E5%8A%9F%EF%BC%9ACSS%E5%92%8CDOM%E7%BB%83%E4%B9%A0/07-DOM%E6%93%8D%E4%BD%9C%E7%BB%83%E4%B9%A0%EF%BC%9AinnerHTML%E7%9A%84%E6%96%B9%E5%BC%8F%E5%88%9B%E5%BB%BA%E5%85%83%E7%B4%A0/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2008-08-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="CSS和DOM">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BADOM%E5%85%83%E7%B4%A0%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="toc-text">动态创建DOM元素的三种方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#innerHTML%E4%B8%BE%E4%BE%8B%EF%BC%9A%E5%9C%A8%E7%BA%BF%E7%94%A8%E6%88%B7%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="toc-text">innerHTML举例：在线用户的获取</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#innerHTML%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E6%A8%A1%E6%8B%9F%E7%99%BE%E5%BA%A6%E6%90%9C%E7%B4%A2%E7%9A%84%E4%B8%8B%E6%96%B9%E6%8F%90%E7%A4%BA"><span class="toc-text">innerHTML举例2：模拟百度搜索的下方提示</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E6%93%8D%E4%BD%9C%E8%A1%A8%E6%A0%BC"><span class="toc-text">动态操作表格</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      07-DOM操作练习：innerHTML的方式创建元素
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2008-07-31T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2008-08-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/CSS%E5%92%8CDOM/" 
          class="post-meta-link">
          CSS和DOM
        </a>
      
    
    
      <span class="dot"></span>
      <span>3k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/CSS%E5%92%8CDOM/" 
            class="post-meta-link">
            CSS和DOM
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="动态创建DOM元素的三种方式"><a href="#动态创建DOM元素的三种方式" class="headerlink" title="动态创建DOM元素的三种方式"></a>动态创建DOM元素的三种方式</h2><ul>
<li><p><code>document.write();</code> 不常用，因为容易覆盖原来的页面。</p>
</li>
<li><p><code>innerHTML = ();</code> 用的比较多。绑定属性和内容比较方便。(节点套节点)</p>
</li>
<li><p><code>document.createElement();</code> 用得也比较多，指定数量的时候一般用它。</p>
</li>
</ul>
<p><strong>1、方式一：</strong></p>
<pre class="highlight"><span class="line"><span class="variable language_">document</span>.<span class="title function_">write</span>();</span><br></pre>

<p>这种方式的好处是：比较随意，想创建就创建，可以直接在<code>write</code>里写属性都行。但是会把原来的标签给覆盖掉。所以不建议。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    smyhvae</span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//第一种创建方式：document.write();</span></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">document</span>.<span class="title function_">write</span>(<span class="string">&quot;&lt;li class=&#x27;hehe&#x27;&gt;我是document.write创建的&lt;/li&gt;&quot;</span>);</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180129_1908.png" class="lozad post-image"src="http://img.smyhvae.com/20180129_1908.png"></p>
<p><strong>方式二：</strong>innerHTML</p>
<p>举例如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    smyhvae</span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> ul = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;ul&quot;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//第二种：直接利用元素的innerHTNL方法。（innerText方法不识别标签）</span></span></span><br><span class="line"><span class="language-javascript">    ul.<span class="property">innerHTML</span> += <span class="string">&quot;&lt;li id=&#x27;li1&#x27;&gt;我是innerHTML创建的&lt;/li&gt;&quot;</span>  <span class="comment">//注意，是用符号“+=”，不是“=”</span></span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>注意，上方代码中，是用是用符号<code>+=</code>，不是<code>=</code>，前者是在原来的基础之上增加，后者是替换。</p>
<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180129_2017.png" class="lozad post-image"src="http://img.smyhvae.com/20180129_2017.png"></p>
<p><strong>3、方式三：</strong>利用DOM的api创建</p>
<p>利用DOM的api创建节点，有很多种：</p>
<p>比如：</p>
<ul>
<li><p>createElement()</p>
</li>
<li><p>appendChild()</p>
</li>
<li><p>removeChild()</p>
</li>
<li><p>insertBefore()</p>
</li>
<li><p>replaceChild()</p>
</li>
</ul>
<p>这个我们在上一篇文章的<code>DOM节点的操作</code>这一段中已经讲到了。</p>
<pre class="highlight"><span class="line"></span><br></pre>


<h2 id="innerHTML举例：在线用户的获取"><a href="#innerHTML举例：在线用户的获取" class="headerlink" title="innerHTML举例：在线用户的获取"></a>innerHTML举例：在线用户的获取</h2><p>现在要做下面这样一个页面：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180129_2151.png" class="lozad post-image"src="http://img.smyhvae.com/20180129_2151.png"></p>
<p>上图的意思是，每次刷新页面，都从服务器获取最新的在线人数的名单（我们先用本地的数组来模拟服务器上的数据）。</p>
<p>它的结构是：div &gt; ul &gt; li。每一个li就是一个头像。</p>
<p>如果在本地直接添加几个头像的话，代码是：</p>
<pre class="highlight"><span class="line">//往ul中添加li元素以及li元素中的内容</span><br><span class="line">   ul.innerHTML += &#x27;<span class="tag">&lt;<span class="name">li</span>&gt;</span>&#x27;+</span><br><span class="line">                   &#x27;<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">target</span>=<span class="string">&quot;_blank&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/noavatar_small.gif&quot;</span> <span class="attr">width</span>=<span class="string">&quot;48&quot;</span> <span class="attr">height</span>=<span class="string">&quot;48&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;千古壹号&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span>&#x27;+</span><br><span class="line">                   &#x27;<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">title</span>=<span class="string">&quot;千古壹号&quot;</span> <span class="attr">target</span>=<span class="string">&quot;_blank&quot;</span>&gt;</span>千古壹号<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span>&#x27;+</span><br><span class="line">               &#x27;<span class="tag">&lt;/<span class="name">li</span>&gt;</span>&#x27;;</span><br><span class="line">   ul.innerHTML += &#x27;<span class="tag">&lt;<span class="name">li</span>&gt;</span>&#x27;+</span><br><span class="line">           &#x27;<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">target</span>=<span class="string">&quot;_blank&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/noavatar_small.gif&quot;</span> <span class="attr">width</span>=<span class="string">&quot;48&quot;</span> <span class="attr">height</span>=<span class="string">&quot;48&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;千古壹号&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span>&#x27;+</span><br><span class="line">           &#x27;<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">title</span>=<span class="string">&quot;千古壹号&quot;</span> <span class="attr">target</span>=<span class="string">&quot;_blank&quot;</span>&gt;</span>千古壹号<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span>&#x27;+</span><br><span class="line">           &#x27;<span class="tag">&lt;/<span class="name">li</span>&gt;</span>&#x27;;</span><br><span class="line">   ul.innerHTML += &#x27;<span class="tag">&lt;<span class="name">li</span>&gt;</span>&#x27;+</span><br><span class="line">           &#x27;<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">target</span>=<span class="string">&quot;_blank&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/noavatar_small.gif&quot;</span> <span class="attr">width</span>=<span class="string">&quot;48&quot;</span> <span class="attr">height</span>=<span class="string">&quot;48&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;千古壹号&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span>&#x27;+</span><br><span class="line">           &#x27;<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">title</span>=<span class="string">&quot;千古壹号&quot;</span> <span class="attr">target</span>=<span class="string">&quot;_blank&quot;</span>&gt;</span>千古壹号<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span>&#x27;+</span><br><span class="line">           &#x27;<span class="tag">&lt;/<span class="name">li</span>&gt;</span>&#x27;;</span><br><span class="line"></span><br></pre>

<p>上方代码有两点比较重要：</p>
<ul>
<li><p>我们是通过<code>ul.innerHTML += 元素节点</code>的方式来不停地往ul里面加内容，比<code>createElement</code>的方式要方便。</p>
</li>
<li><p>元素的内容本身有双引号<code>&quot;</code>，所以我们要用单引号<code>&#39;</code>进行字符串的连接。</p>
</li>
</ul>
<p>但是，当我们从服务器获取在线用户的时候，头像和用户的昵称是动态变化的，所以每个字符串要用变量进行表示：</p>
<pre class="highlight"><span class="line">ul.innerHTML += &#x27;<span class="tag">&lt;<span class="name">li</span>&gt;</span>&#x27;+</span><br><span class="line">                    &#x27;<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">target</span>=<span class="string">&quot;blank&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;&#x27;+users[i].icon+&#x27;&quot;</span> <span class="attr">width</span>=<span class="string">&quot;48&quot;</span> <span class="attr">height</span>=<span class="string">&quot;48&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;&#x27;+users[i].name+&#x27;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span>&#x27;+</span><br><span class="line">                    &#x27;<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">title</span>=<span class="string">&quot;&#x27;+users[i].name+&#x27;&quot;</span> <span class="attr">target</span>=<span class="string">&quot;_blank&quot;</span>&gt;</span>&#x27;+users[i].name+&#x27;<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span>&#x27;+</span><br><span class="line">                &#x27;<span class="tag">&lt;/<span class="name">li</span>&gt;</span>&#x27;;</span><br><span class="line"></span><br></pre>

<p>这里我们暂时用本地的数组来代表服务器的数据，最终的完整版代码如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        * &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">word-wrap</span>: break-word;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.wp</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">730px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0px</span> auto;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.mtn</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin-top</span>: <span class="number">5px</span> <span class="meta">!important</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-id">#ct</span> <span class="selector-class">.frame</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: none;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.xfs_2</span> <span class="selector-class">.frame-title</span>, <span class="selector-class">.xfs_2</span> <span class="selector-class">.frametitle</span>, <span class="selector-class">.xfs_2</span> <span class="selector-class">.tab-title</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: <span class="number">#A90000</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-position</span>: <span class="number">0</span> -<span class="number">99px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.xfs</span> <span class="selector-class">.frame-title</span>, <span class="selector-class">.xfs</span> <span class="selector-class">.frametitle</span>, <span class="selector-class">.xfs</span> <span class="selector-class">.tab-title</span>, <span class="selector-class">.xfs</span> <span class="selector-class">.frame-title</span> <span class="selector-tag">a</span>, <span class="selector-class">.xfs</span> <span class="selector-class">.frametitle</span> <span class="selector-tag">a</span>, <span class="selector-class">.xfs</span> <span class="selector-class">.tab-title</span> <span class="selector-tag">a</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">color</span>: <span class="number">#FFF</span> <span class="meta">!important</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.xfs</span> <span class="selector-class">.frame-title</span>, <span class="selector-class">.xfs</span> <span class="selector-class">.frametitle</span>, <span class="selector-class">.xfs</span> <span class="selector-class">.tab-title</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: none;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background</span>: transparent <span class="built_in">url</span>(<span class="string">images/mu.png</span>) repeat-x <span class="number">0</span> <span class="number">95</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.title</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">0</span> <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">32px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">font-size</span>: <span class="number">14px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">font-weight</span>: <span class="number">700</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">line-height</span>: <span class="number">32px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">overflow</span>: hidden;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.block</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">10px</span> <span class="number">10px</span> <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">ul</span>, <span class="selector-tag">menu</span>, dir &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">display</span>: block;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">list-style</span>: none;</span></span><br><span class="line"><span class="language-css">            -webkit-<span class="attribute">margin</span>-before: <span class="number">1em</span>;</span></span><br><span class="line"><span class="language-css">            -webkit-<span class="attribute">margin</span>-after: <span class="number">1em</span>;</span></span><br><span class="line"><span class="language-css">            -webkit-<span class="attribute">margin</span>-start: <span class="number">0px</span>;</span></span><br><span class="line"><span class="language-css">            -webkit-<span class="attribute">margin</span>-end: <span class="number">0px</span>;</span></span><br><span class="line"><span class="language-css">            -webkit-<span class="attribute">padding</span>-start: <span class="number">25px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.mls</span> <span class="selector-tag">li</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">0</span> <span class="number">0</span> <span class="number">5px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">66px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">85px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.ml</span> <span class="selector-tag">li</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">overflow</span>: hidden;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">a</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">color</span>: <span class="number">#333</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-decoration</span>: none;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">font</span>: <span class="number">12px</span>/<span class="number">1.5</span> Tahoma, <span class="string">&#x27;Microsoft Yahei&#x27;</span>, <span class="string">&#x27;Simsun&#x27;</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.mls</span> <span class="selector-tag">p</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin-top</span>: <span class="number">5px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.ml</span> <span class="selector-tag">p</span>, <span class="selector-class">.ml</span> <span class="selector-tag">span</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">display</span>: block;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">white-space</span>: nowrap;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-overflow</span>: ellipsis;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">overflow</span>: hidden;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.mls</span> <span class="selector-tag">img</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">48px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">48px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.ml</span> <span class="selector-tag">img</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">display</span>: block;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span> auto;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">a</span> <span class="selector-tag">img</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: none;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;wp mtn&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;diy3&quot;</span> <span class="attr">class</span>=<span class="string">&quot;area&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;frameipq7f2&quot;</span> <span class="attr">class</span>=<span class="string">&quot;xfs xfs_2 frame move-span cl frame-1&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span></span></span><br><span class="line"><span class="tag">                    <span class="attr">class</span>=<span class="string">&quot;title frame-title&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;titletext&quot;</span>&gt;</span>当前在线用户<span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;frameipq7f2_left&quot;</span></span></span><br><span class="line"><span class="tag">                 <span class="attr">class</span>=<span class="string">&quot;column frame-1-c&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">div</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">id</span>=<span class="string">&quot;frameipq7f2_left_temp&quot;</span> <span class="attr">class</span>=<span class="string">&quot;move-span temp&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;portal_block_695&quot;</span></span></span><br><span class="line"><span class="tag">                     <span class="attr">class</span>=<span class="string">&quot;block move-span&quot;</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">div</span></span></span><br><span class="line"><span class="tag">                            <span class="attr">id</span>=<span class="string">&quot;portal_block_695_content&quot;</span> <span class="attr">class</span>=<span class="string">&quot;dxb_bc&quot;</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;module cl ml mls&quot;</span> <span class="attr">id</span>=<span class="string">&quot;users&quot;</span>&gt;</span></span><br><span class="line">                            <span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">                                <span class="comment">&lt;!--&lt;li&gt;--&gt;</span></span><br><span class="line">                                <span class="comment">&lt;!--&lt;a href=&quot;#&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;images/noavatar_small.gif&quot; width=&quot;48&quot; height=&quot;48&quot; alt=&quot;千古壹号&quot;&gt;&lt;/a&gt;--&gt;</span></span><br><span class="line">                                <span class="comment">&lt;!--&lt;p&gt;&lt;a href=&quot;#&quot; title=&quot;千古壹号&quot; target=&quot;_blank&quot;&gt;千古壹号&lt;/a&gt;&lt;/p&gt;--&gt;</span></span><br><span class="line">                                <span class="comment">&lt;!--&lt;/li&gt;--&gt;</span></span><br><span class="line"></span><br><span class="line">                            <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//模拟从服务器获取数据</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> users = [</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;smyhvae&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;smyh&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;smyh02&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/75_avatar_small.jpg&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;vae&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/89_avatar_small.jpg&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;today&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;enen&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;oyey&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;dongxiaojie&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;qishi&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;qqtang&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;wawawa&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;haha&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;robot&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;heheda&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;smyhvae1&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;,</span></span><br><span class="line"><span class="language-javascript">        &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;lihaile&quot;</span>, <span class="string">&quot;icon&quot;</span>: <span class="string">&quot;images/noavatar_small.gif&quot;</span>&#125;</span></span><br><span class="line"><span class="language-javascript">    ];</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//需求：页面显示所有的在线用户。</span></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//思路：模拟服务器获取数据（数组中装着json）.获取ul，把ul的innerHTML属性获取到，然后不间断的往innerHTML属性中赋值。</span></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//赋值要求：li标签的内容。</span></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//步骤：(获取元素)</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> div = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;users&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> ul = div.<span class="property">firstElementChild</span> || div.<span class="property">firstChild</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//        var ul = div.children[0];</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//1.模拟服务器获取数据（定义数组），通过循环添加元素（定义for）</span></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//数组中有多少元素，我们就创建多少个li标签</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; users.<span class="property">length</span>; i++) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//2.模拟实验的操作方式。</span></span></span><br><span class="line"><span class="language-javascript">        ul.<span class="property">innerHTML</span> += <span class="string">&#x27;&lt;li&gt;&#x27;</span> +</span></span><br><span class="line"><span class="language-javascript">            <span class="string">&#x27;&lt;a href=&quot;#&quot; target=&quot;blank&quot;&gt;&lt;img src=&quot;&#x27;</span> + users[i].<span class="property">icon</span> + <span class="string">&#x27;&quot; width=&quot;48&quot; height=&quot;48&quot; alt=&quot;&#x27;</span> + users[i].<span class="property">name</span> + <span class="string">&#x27;&quot;&gt;&lt;/a&gt;&#x27;</span> +</span></span><br><span class="line"><span class="language-javascript">            <span class="string">&#x27;&lt;p&gt;&lt;a href=&quot;#&quot; title=&quot;&#x27;</span> + users[i].<span class="property">name</span> + <span class="string">&#x27;&quot; target=&quot;_blank&quot;&gt;&#x27;</span> + users[i].<span class="property">name</span> + <span class="string">&#x27;&lt;/a&gt;&lt;/p&gt;&#x27;</span> +</span></span><br><span class="line"><span class="language-javascript">            <span class="string">&#x27;&lt;/li&gt;&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>工程文件：<a target="_blank" rel="noopener" href="https://github.com/qianguyihao/web-resource/blob/main/project/2018-02-01-%E8%8E%B7%E5%8F%96%E5%9C%A8%E7%BA%BF%E7%94%A8%E6%88%B7%E5%88%97%E8%A1%A8demo.rar">2018-02-01-获取在线用户列表demo.rar</a></p>
<h2 id="innerHTML举例2：模拟百度搜索的下方提示"><a href="#innerHTML举例2：模拟百度搜索的下方提示" class="headerlink" title="innerHTML举例2：模拟百度搜索的下方提示"></a>innerHTML举例2：模拟百度搜索的下方提示</h2><p>要求实现的效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180201_2030.gif" class="lozad post-image"src="http://img.smyhvae.com/20180201_2030.gif"></p>
<p>在这之前，我们先实现这样一个例子：<strong>判断字符串以某个字符串为开头</strong>。</p>
<p><strong>判断字符串是否以某个字符串为开头：</strong></p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> str = <span class="string">&quot;smyhvae&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//判断str是否以sm为开头？（给定字符串，然后他的索引值为0）</span></span><br><span class="line"><span class="keyword">var</span> num = str.<span class="title function_">indexOf</span>(<span class="string">&quot;sm&quot;</span>);</span><br><span class="line"><span class="comment">//只有返回值为0，那么字符串才是以参数为开头</span></span><br><span class="line"><span class="comment">//如果在任何位置都查询不到参数，则返回值为-1；</span></span><br><span class="line"></span><br></pre>


<p>代码解释：我们可以通过<code>indexOf(&quot;参数&quot;)</code>来实现。如果返回的索引值为0，说明字符串就是以这个参数开头的。</p>
<p>为了实现上方gif图的搜索功能，完整版代码如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        * &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.box</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">500px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">200px</span> auto;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">ul</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">392px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">5px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">list-style</span>: none;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">li</span><span class="selector-pseudo">:hover</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: red;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">input</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">button</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">70px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span>&gt;</span>搜索<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;ul&gt;--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;li&gt;aaaa&lt;/li&gt;--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;li&gt;bbb&lt;/li&gt;--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;li&gt;ccc&lt;/li&gt;--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;/ul&gt;--&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//需求：输入内容(输入事件，键盘弹起事件)，模拟服务器获取内容，创建ul，并在其中显示。</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//1.获取事件源</span></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//模拟服务器获取内容</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> arr = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;ab&quot;</span>, <span class="string">&quot;abc&quot;</span>, <span class="string">&quot;abcd&quot;</span>, <span class="string">&quot;aa&quot;</span>, <span class="string">&quot;aaa&quot;</span>];</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> box = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;div&quot;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> inp = box.<span class="property">children</span>[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//        var inp = document.getElementsByTagName(&quot;input&quot;)[0];</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//2.绑定事件(输入内容(输入事件，键盘弹起事件))</span></span></span><br><span class="line"><span class="language-javascript">    inp.<span class="property">onkeyup</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//创建一个字符串，里面添加满了li和对应的内容。</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">var</span> newArr = [];</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//遍历老数组arr，然后判断每一项，只要是以input的内容为开头的，就添加到新数组newArr中，然后转成字符串。</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.<span class="property">length</span>; i++) &#123;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//获取输入内容input标签的value属性值。</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">if</span> (arr[i].<span class="title function_">indexOf</span>(<span class="variable language_">this</span>.<span class="property">value</span>) === <span class="number">0</span>) &#123;  <span class="comment">//【重要】判断老数组arr中的每一项，是否以input的内容为开头</span></span></span><br><span class="line"><span class="language-javascript">                newArr.<span class="title function_">push</span>(<span class="string">&quot;&lt;li&gt;&quot;</span> + arr[i] + <span class="string">&quot;&lt;/li&gt;&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">var</span> str = newArr.<span class="title function_">join</span>(<span class="string">&quot;&quot;</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//Bug1：每次创建新的ul之前，如果有就的ul，就先删除旧的ul</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (box.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;ul&quot;</span>)[<span class="number">0</span>]) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//只要存在，那么就是object，object类型的数据，只要不是null,对应的boolean值都是true；</span></span></span><br><span class="line"><span class="language-javascript">            box.<span class="title function_">removeChild</span>(box.<span class="property">children</span>[<span class="number">2</span>]);</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//Bug2.如果input的内容为空，那么就不能再生成ul了。</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//如果input为空，那就切断函数</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//Bug3.如果arr数组中找不到以input为开头的元素。那就切断函数</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//newArr的长度为0，就能证明以input内容为开头的元素，在arr中不存在</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">value</span>.<span class="property">length</span> === <span class="number">0</span> || newArr.<span class="property">length</span> === <span class="number">0</span>) &#123;  <span class="comment">//fix bug2、fix bug3</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//切断函数，直接return</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">return</span>;</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//3.书写事件驱动程序</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">var</span> ul = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;ul&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//把创建好的内容添加到ul中。</span></span></span><br><span class="line"><span class="language-javascript">        ul.<span class="property">innerHTML</span> = str;</span></span><br><span class="line"><span class="language-javascript">        box.<span class="title function_">appendChild</span>(ul);</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>



<h2 id="动态操作表格"><a href="#动态操作表格" class="headerlink" title="动态操作表格"></a>动态操作表格</h2><p>方式1：</p>
<pre class="highlight"><span class="line">createElement()</span><br></pre>

<p>方式2：</p>
<ul>
<li><p>rows                          (只读，table和textarea能用)</p>
</li>
<li><p>insertRow()              (只有table能调用)</p>
</li>
<li><p>deleteRow()           (只有table能调用)</p>
</li>
<li><p>cells             (只读，table和textarea能用)</p>
</li>
<li><p>insertCell()               (只有tr能调用)</p>
</li>
<li><p>deleteCell()              (只有tr能调用)</p>
</li>
</ul>
<p>PS：括号里可以带index。用的不多。</p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2008/08/01/08-%E5%89%8D%E7%AB%AF%E5%9F%BA%E6%9C%AC%E5%8A%9F%EF%BC%9ACSS%E5%92%8CDOM%E7%BB%83%E4%B9%A0/07-DOM%E6%93%8D%E4%BD%9C%E7%BB%83%E4%B9%A0%EF%BC%9AinnerHTML%E7%9A%84%E6%96%B9%E5%BC%8F%E5%88%9B%E5%BB%BA%E5%85%83%E7%B4%A0/">
            https://zwl5670.gitee.io/2008/08/01/08-%E5%89%8D%E7%AB%AF%E5%9F%BA%E6%9C%AC%E5%8A%9F%EF%BC%9ACSS%E5%92%8CDOM%E7%BB%83%E4%B9%A0/07-DOM%E6%93%8D%E4%BD%9C%E7%BB%83%E4%B9%A0%EF%BC%9AinnerHTML%E7%9A%84%E6%96%B9%E5%BC%8F%E5%88%9B%E5%BB%BA%E5%85%83%E7%B4%A0/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2008/08/01/08-%E5%89%8D%E7%AB%AF%E5%9F%BA%E6%9C%AC%E5%8A%9F%EF%BC%9ACSS%E5%92%8CDOM%E7%BB%83%E4%B9%A0/05-DOM%E6%93%8D%E4%BD%9C%E7%BB%83%E4%B9%A0%EF%BC%9A%E8%AE%BF%E9%97%AE%E5%85%B3%E7%B3%BB%E7%9A%84%E5%B0%81%E8%A3%85/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">05-DOM操作练习：访问关系的封装 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2007/07/01/07-JavaScript%E8%BF%9B%E9%98%B6/01-var%E3%80%81let%E3%80%81const%E7%9A%84%E5%8C%BA%E5%88%AB/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">01-var、let、const的区别 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BADOM%E5%85%83%E7%B4%A0%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="toc-text">动态创建DOM元素的三种方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#innerHTML%E4%B8%BE%E4%BE%8B%EF%BC%9A%E5%9C%A8%E7%BA%BF%E7%94%A8%E6%88%B7%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="toc-text">innerHTML举例：在线用户的获取</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#innerHTML%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E6%A8%A1%E6%8B%9F%E7%99%BE%E5%BA%A6%E6%90%9C%E7%B4%A2%E7%9A%84%E4%B8%8B%E6%96%B9%E6%8F%90%E7%A4%BA"><span class="toc-text">innerHTML举例2：模拟百度搜索的下方提示</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E6%93%8D%E4%BD%9C%E8%A1%A8%E6%A0%BC"><span class="toc-text">动态操作表格</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BADOM%E5%85%83%E7%B4%A0%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="toc-text">动态创建DOM元素的三种方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#innerHTML%E4%B8%BE%E4%BE%8B%EF%BC%9A%E5%9C%A8%E7%BA%BF%E7%94%A8%E6%88%B7%E7%9A%84%E8%8E%B7%E5%8F%96"><span class="toc-text">innerHTML举例：在线用户的获取</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#innerHTML%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E6%A8%A1%E6%8B%9F%E7%99%BE%E5%BA%A6%E6%90%9C%E7%B4%A2%E7%9A%84%E4%B8%8B%E6%96%B9%E6%8F%90%E7%A4%BA"><span class="toc-text">innerHTML举例2：模拟百度搜索的下方提示</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E6%93%8D%E4%BD%9C%E8%A1%A8%E6%A0%BC"><span class="toc-text">动态操作表格</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
